package com.fr.web.core.chwriter;

import com.fr.base.ColumnRow;
import com.fr.base.Style;
import com.fr.base.background.ColorBackground;
import com.fr.base.core.html.Tag;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.data.core.db.BinaryObject;
import com.fr.report.CellElement;
import com.fr.report.ReportGetter;
import com.fr.report.cellElement.CellGUIAttr;
import com.fr.report.cellElement.Formula;
import com.fr.report.cellElement.core.CellElementAttribute;
import com.fr.report.js.NameJavaScriptGroup;
import com.fr.report.web.ui.Widget;
import com.fr.util.Utils;
import com.fr.web.Repository;
import com.fr.web.core.HTMLWriterUtils;
import com.fr.web.core.SessionIDInfor;
import java.awt.Rectangle;
import java.sql.Blob;
import java.sql.Clob;

public abstract class CellHtmlWriter
{
  protected Repository repo;
  protected Tag resTag;
  protected int cellWidth;
  protected int cellHeight;
  protected CellGUIAttr cellGUIAttr;
  protected boolean heavy = false;
  protected int reportIndex;
  protected int tableID = 0;

  public CellHtmlWriter(Repository paramRepository, int paramInt)
  {
    this.repo = paramRepository;
    this.reportIndex = paramInt;
  }

  public Tag cell2Tag(CellElement paramCellElement, ReportGetter paramReportGetter, Rectangle paramRectangle1, Rectangle paramRectangle2)
    throws JSONException
  {
    this.resTag = new Tag("td");
    if (paramRectangle1.width > 1)
      this.resTag.attr("colSpan", paramRectangle1.width + "");
    if (paramRectangle1.height > 1)
      this.resTag.attr("rowSpan", paramRectangle1.height + "");
    if (paramReportGetter.getColumnWidth(paramCellElement.getColumn()) == 0)
      this.resTag.css("display", "none");
    this.cellHeight = 0;
    int i = paramRectangle2.y;
    for (int j = 0; j < paramRectangle2.height; ++j)
    {
      this.cellHeight += paramReportGetter.getRowHeight(i);
      ++i;
    }
    this.cellWidth = 0;
    i = paramRectangle2.x;
    for (j = 0; j < paramRectangle2.width; ++j)
    {
      this.cellWidth += paramReportGetter.getColumnWidth(i);
      ++i;
    }
    this.cellGUIAttr = paramCellElement.getCellGUIAttr();
    if (this.cellGUIAttr == null)
      this.cellGUIAttr = CellGUIAttr.DEFAULT_CELLGUIATTR;
    String str = this.cellGUIAttr.getTooltipText();
    if (str != null)
      this.resTag.attr("title", str);
    return dealWithHyperlinkGroup(paramCellElement, this.resTag);
  }

  protected Tag dealWithHyperlinkGroup(CellElement paramCellElement, Tag paramTag)
    throws JSONException
  {
    NameJavaScriptGroup localNameJavaScriptGroup = paramCellElement.getNameHyperlinkGroup();
    if ((localNameJavaScriptGroup != null) && (localNameJavaScriptGroup.size() > 0))
    {
      Style localStyle = paramCellElement.getStyle();
      Tag localTag = paramTag;
      if ((localStyle.getBackground() == null) || (localStyle.getBackground() instanceof ColorBackground))
      {
        localTag = new Tag("span");
        paramTag.sub(localTag);
      }
      localTag.css("cursor", "pointer");
      localTag.attr("onclick", "FR.doHyperlink(event||window.event, " + HTMLWriterUtils.writeJSLinkContent(localNameJavaScriptGroup, this.repo) + ", true)");
      paramTag.cls("celink");
      localTag.cls("linkspan");
      return localTag;
    }
    return paramTag;
  }

  public void dealWithEmptyHeight(Tag paramTag)
  {
    paramTag.css("display", "none");
  }

  protected String widget2Config(Widget paramWidget, Object paramObject, ColumnRow paramColumnRow, CellElement paramCellElement, int paramInt, String paramString)
    throws JSONException
  {
    JSONObject localJSONObject = paramWidget.createJSONConfig(paramObject, this.repo);
    localJSONObject.put("widgetUrl", this.repo.checkoutWidgetUrl(new JSONObject().put("reportIndex", paramInt).put("column", paramColumnRow.getColumn()).put("row", paramColumnRow.getRow()), paramString));
    localJSONObject.put("sessionID", this.repo.getSessionIDInfor().getSessionID());
    localJSONObject.put("location", ColumnRow.valueOf(paramColumnRow.getColumn(), paramColumnRow.getRow()));
    localJSONObject.put("reportIndex", paramInt);
    if (paramCellElement.getAttribute(CellElementAttribute.WIDGET_DEPENDENCE) != null)
      localJSONObject.put("dependenceMap", paramCellElement.getAttribute(CellElementAttribute.WIDGET_DEPENDENCE));
    return localJSONObject.toString();
  }

  protected Object dealWithValue4Formula(Object paramObject)
  {
    return ((Formula)paramObject).getResult();
  }

  protected Object dealWithValue4Blob(Object paramObject)
  {
    return Utils.blob2Object((Blob)paramObject, this.cellGUIAttr.isShowAsImage());
  }

  protected Object dealBinaryObject(Object paramObject)
  {
    if (this.cellGUIAttr.isShowAsImage())
      return ((BinaryObject)paramObject).getImage();
    return ((BinaryObject)paramObject).getValue();
  }

  protected String dealClobValue(Object paramObject)
  {
    return Utils.clob2String((Clob)paramObject);
  }

  public void setHeavy(boolean paramBoolean)
  {
    this.heavy = paramBoolean;
  }

  public boolean isHeavy()
  {
    return this.heavy;
  }

  public void setTableID(int paramInt)
  {
    this.tableID = paramInt;
  }

  public int getTableID()
  {
    return this.tableID;
  }
}